package com.mxpio.erp.workshop.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.mxpio.erp.common.inventory.entity.InvFlow;
import com.mxpio.erp.common.workshop.entity.ManufactureOrder;
import com.mxpio.erp.common.workshop.service.ManufactureOrderService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ManufactureOrderServiceImpl extends BaseServiceImpl<ManufactureOrder> implements ManufactureOrderService {

	@Override
	@Transactional
	public void handleManufactureOrderQuantity(List<ManufactureOrder> content, String accessType) {
		Map<String, BigDecimal> quantity = new HashMap<>();
		Set<String> bizNos = JpaUtil.collectId(content);
		//1.通过InvFlow计算实际执行数量
		List<InvFlow> flows = JpaUtil.linq(InvFlow.class).in("originBizNo", bizNos).list();
		for(InvFlow flow : flows){
			
			//  若流水的AccessType为参数accessType 则为执行，否则为退货
			if(accessType.equals(flow.getAccessType())){
				BigDecimal actualQuantity = quantity.get("actual:"+flow.getOriginBizNo()+flow.getOriginBizLineNo());
				if(actualQuantity == null){
					actualQuantity = BigDecimal.ZERO;
				}
				actualQuantity = actualQuantity.add(flow.getQuantity());
				quantity.put("actual:"+flow.getOriginBizNo()+flow.getOriginBizLineNo(), actualQuantity);
			}else{
				BigDecimal actualRejectQuantity = quantity.get("actualReject:"+flow.getOriginBizNo()+flow.getOriginBizLineNo());
				if(actualRejectQuantity == null){
					actualRejectQuantity = BigDecimal.ZERO;
				}
				actualRejectQuantity = actualRejectQuantity.add(flow.getQuantity());
				quantity.put("actualReject:"+flow.getOriginBizNo()+flow.getOriginBizLineNo(), actualRejectQuantity);
			}
			
		}
		
	}

}
